package iot.queue;

import iot.cassandra.CassLocationDao;
import iot.cassandra.CassRegistrationDao;
import iot.entity.Location;
import iot.entity.Registration;
import iot.hbase.LocationAsyncDao;
import iot.hbase.LocationDao;
import iot.hbase.RegistrationAsyncDao;
import iot.hbase.RegistrationDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by dave on 18-7-27 上午11:17.
 */
//@Configuration
public class JTT808DaoConfig {
    @Autowired(required = false) LocationAsyncDao locationAsyncDao;
    @Autowired(required = false) RegistrationAsyncDao registrationAsyncDao;
    @Autowired(required = false) LocationDao locationDao;
    @Autowired(required = false) RegistrationDao registrationDao;
    @Autowired(required = false) CassLocationDao cassLocationDao;
    @Autowired(required = false) CassRegistrationDao cassRegistrationDao;

    @Bean
    public DataQueueDispatcher queueDispatcher() {
        return new DataQueueDispatcher();
    }

    @Bean
    public DataQueue<Registration> regQueue(DataQueueDispatcher dispatcher) {
        return new DataQueue<>(Registration.class, list -> {
            if (registrationDao != null) {
                registrationDao.batchInsert(list);
            } else if (registrationAsyncDao != null) {
                registrationAsyncDao.batchInsert(list);
            } else {
                cassRegistrationDao.save(list);
            }
        }, dispatcher.getExecutor());
    }

    @Bean
    public DataQueue<Location> locQueue(DataQueueDispatcher dispatcher) {
        return new DataQueue<>(Location.class, list -> {
            if (locationDao != null) {
                locationDao.batchInsert(list);
            } else if (locationAsyncDao != null) {
                locationAsyncDao.batchInsert(list);
            } else {
                cassLocationDao.save(list);
            }
        }, dispatcher.getExecutor());
    }
}
